package tankbb;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class TankClient extends Frame{
	
	public static final int WIN_WIDTH = 1500;
	public static final int WIN_HEIGH = 650;
	public static final int TANK_POS_X = 200;
	public static final int TANK_POS_Y = 200;
	public static enum Dir{
		U, UR, R, DR, D, LD, L, LU;
		
		public static Dir randomDir(){
			Random r = new Random();
			return values()[r.nextInt(values().length)];
		}
	};
	
	Image bgBuf = null;
	Tank t1 = null;
	public ArrayList<Missile> arr_m = null;
	public ArrayList<Tank> arr_t = null;
	public ArrayList<Explode> arr_e = null;
	int cnt;
	Graphics g1 = this.getGraphics();
	Explode e = new Explode(300, 300, this);
		
	
	public void paint(Graphics g) {
		Color c = g.getColor();
		g.setColor(Color.RED);
		g.drawString("missile : " + arr_m.size(), 10, 50);
		g.drawString("tank : " + arr_t.size(), 10, 75);
		g.drawString("index_m : " + Tank.index_m, 10, 100);
		g.drawString("index_t : " + Tank.index_t, 10, 125);
		g.setColor(c);
		
		if(arr_t != null){
			for(int i = 0; i < arr_t.size(); i++){
				Iterator<Tank> it = arr_t.iterator();
				while(it.hasNext()){
					Tank t = it.next();
					if(t.isLive()){
						t.draw(g);
					}else{
						it.remove();
					}
				}
			}
		}
		
		if(arr_m != null){
			for(int i = 0; i < arr_m.size(); i++){
				Iterator<Missile> im = arr_m.iterator();
				while(im.hasNext()){
					Missile m = im.next();
					if(m.isLive()){
						m.draw(g);
					}else{
						im.remove();
					}
				}
			}
		}
		
		if(arr_e != null){
			for(int i = 0; i < arr_e.size(); i++){
				Iterator<Explode> ie = arr_e.iterator();
				while(ie.hasNext()){
					Explode e = ie.next();
					if(e.isLive()){
						e.draw(g);
					}else{
						ie.remove();
					}
				}
			}
		}
	}

	public void update(Graphics g) {
		if(bgBuf == null){
			bgBuf = this.createImage(WIN_WIDTH, WIN_HEIGH);
		}
		
		Graphics bgG = bgBuf.getGraphics();
		Color c = bgG.getColor();
		bgG.setColor(Color.GRAY);
		bgG.fillRect(0, 0, WIN_WIDTH, WIN_HEIGH);
		paint(bgG);
		g.drawImage(bgBuf, 0, 0, null);
		bgG.setColor(c);
	}

	void launchFrame(){
		this.setBounds(200, 50, WIN_WIDTH, WIN_HEIGH);
		this.setResizable(false);
		this.setBackground(Color.GRAY);
		this.addWindowListener(new WindowAdapter(){
			
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
			
		});
		this.setVisible(true);
		this.addKeyListener(new ControlKey());
		
		arr_m = new ArrayList<Missile>();
		arr_t = new ArrayList<Tank>();
		arr_e = new ArrayList<Explode>();
		
		t1 = new Tank(TANK_POS_X, TANK_POS_Y, this, true);
		arr_t.add(t1);

		new Thread(new Repaint()).start();
	}
	
	class Repaint implements Runnable{
		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	class ControlKey extends KeyAdapter{
		public void keyPressed(KeyEvent e) {
			t1.keyPressed(e);
		}

		public void keyReleased(KeyEvent e) {
			t1.keyReleased(e);
		}
	}
	
	public static void main(String[] args) {
		new TankClient().launchFrame();
	}

	public void p(Object o){
		System.out.print(o);
	}
}
